package com.allwees.bs.task.order.auto;

import com.allwees.bs.core.model.order.config.ScheduleTimeProperties;
import com.allwees.bs.module.oms.dao.OrderDao;
import com.allwees.bs.module.oms.dao.OrderItemDao;
import com.allwees.bs.module.oms.service.GeneralOmsOrderService;
import com.allwees.core.common.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 发货后xx天内自动收货
 *
 * @author mouhn
 * @since 2019-11-16 14:16
 */

@Slf4j
@Service

public class OrderAutoReceivedOnShippedTask {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderItemDao orderItemDao;
    @Autowired
    private GeneralOmsOrderService generalOmsOrderService;

    @Autowired
    private ScheduleTimeProperties scheduleTimeProperties;

    @XxlJob(value="orderAutoReceivedOnShippedTask")
    public ReturnT<String> run(String s) {
        doTask();
        return ReturnT.SUCCESS;
    }

    private void doTask() {
        if (scheduleTimeProperties.getOrderAutoReceivedOnShippedSecond() == null) {
            return;
        }
        //List<String> orderUuids = orderDao.findExpireReceivedUuids(DateUtil.addSeconds(new Date(), -scheduleTimeProperties.getOrderAutoReceivedOnShippedSecond()));
        List<String> orderItemUuids = orderItemDao.findExpireReceivedUuids(DateUtil.addSeconds(new Date(), -scheduleTimeProperties.getOrderAutoReceivedOnShippedSecond()));
        if (CollectionUtils.isEmpty(orderItemUuids)) {
            return;
        }
        orderItemUuids.forEach(generalOmsOrderService::doReceived);
    }
}
